package com.codebuffer.nowcoder;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class MaxInWindows {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        if(num.length<=0||size<=0){
            return arrayList;
        }
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>((o1, o2) -> o2-o1);

        for (int i = 0; i < num.length; i++) {
            if(i<size-1){
                queue.add(num[i]);
            }else if (i==size-1){
                queue.add(num[i]);
                arrayList.add(queue.peek());
            }else {
                queue.remove(num[i-size]);
                queue.add(num[i]);
                arrayList.add(queue.peek());
            }
        }
        return arrayList;
    }
}
